<?php
require_once("../resources/commons.php");
require_once("finance_commons.php");
require_once("../data/User.php");
require_once("../data/Expense.php");
require_once("../data/Expensetype.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/text.php");
require_once("HTML/QuickForm/textarea.php");;
require_once("HTML/QuickForm/submit.php");
require_once("HTML/QuickForm/select.php");
require_once("HTML/QuickForm/static.php");
require_once("HTML/QuickForm/date.php");

/**
 * Expense creation form.<br/>
 * Form to be used by the finance admin to an add expense to a user.
 */
class AddExpenditurePage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;
        
		$l_userSelect=new HTML_QuickForm_select("user");
		$l_userSelect->setLabel("Account of:");
		$l_records=User::getAll();
		foreach($l_records as $l_user){
			$l_userSelect->addOption($l_user->getFullName(),base64_encode($l_user->getId()));
		}
		$this->addElement($l_userSelect);
        
		$l_expenseTypeSelect=new HTML_QuickForm_select("type");
		$l_expenseTypeSelect->setLabel("Type:");
		$l_records=Expensetype::getAll();
		foreach($l_records as $l_expenseType){
			$l_expenseTypeSelect->addOption($l_expenseType->getLabel(),$l_expenseType->getId());
		}
		$this->addElement($l_expenseTypeSelect);
		
		$l_descriptionInput=new HTML_QuickForm_textarea("description");
		$l_descriptionInput->setLabel("Description:");
		$l_descriptionInput->setCols(60);
		$l_descriptionInput->setRows(5);
		$this->addElement($l_descriptionInput);
		
		$l_createdDateInput=new HTML_QuickForm_date("creationDate",null,
			array(
				"format" => "Y-m-d",
				"maxYear" =>date("Y", time())+2,
				"minYear" =>date("Y", time())));
		$l_createdDateInput->setLabel("When:");
		$this->addElement($l_createdDateInput);
		
		$l_expenseAmountInput=new HTML_QuickForm_text("amount");
		$l_expenseAmountInput->setLabel("Amount (in Euro):");
		$l_expenseAmountInput->setSize(60);
		$this->addElement($l_expenseAmountInput);
		
$l_currencyConverterText=<<<EOT
<b>WARNING:</b> amount must be converted in Euros !<br/>
please use <a href="http://www.xe.com/">http://www.xe.com/</a>

EOT;
		
        $l_currencyConverter=new HTML_Quickform_static("warning");
        $l_currencyConverter->setText($l_currencyConverterText);
        $this->addElement($l_currencyConverter);
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		//The validation rules (either client and server)
		$this->registerRule("checkDate", "callback", "isDateValid");
		$this->addRule("creationDate", "The specified target date is not valid", "checkDate");
		$this->addRule("type","Please provide the type for the purchase","required",null,"server");
		$this->addRule("amount","Please provide the amount of the expense","required",null,"client");
		$this->addRule("amount","Please provide the amount of the expense","required",null,"server");
		$this->addRule("user","On which account is attached the expense ?","required",null,"client");
		$this->addRule("user","On which account is attached the expense ?","required",null,"server");
		$this->addRule("amount","The Amount must be a number (NB: decimal character is \".\")","numeric",null,"server");
		$this->addRule("description","A description must be provided","required",null,"server");
		
		$this->applyFilter("amount","trim");
		$this->applyFilter("description","trim");
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){   	
    	SessionManager::getInstance()->checkSession();
    	checkFinanceAdminRights();
    	displayTop("Modify expense accounts","../resources/htmledit.php",null);
        $page->display();
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.<br>
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	SessionManager::getInstance()->checkSession();
    	checkFinanceAdminRights();
    	
    	$expense=new Expense();
    	$expense->setAmount($page->exportValue("amount"));
    	$expense->setExpensetype_id($page->exportValue("type"));
    	
		$l_creationDate=$page->exportValue("creationDate");
    	$expense->setCreated($l_creationDate["Y"]."/".$l_creationDate["m"]."/".$l_creationDate["d"]);

    	$expense->setDescription($page->exportValue("description"));
    	$l_userId=base64_decode($page->exportValue("user"));
    	$expense->setUser_id($l_userId);
    	
    	$expense->save();
    	
    	HTTP::redirect("manage_expenditure.php");
    }
}

$controller =new HTML_QuickForm_Controller('AddExpenditureProcess', false);
$controller->container(true);
$controller->setDefaults(
	array("creationDate" => date("Y-m-d", time()))
);
$controller->addPage(new AddExpenditurePage('addExpenditurePage'));
$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());
$controller->run();

?>